package demo1;

import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
        //return this.age-o.age;
    }
}
public class Test {
    //自己实现排序
    //冒泡排序
    public static void mySort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length - 1; i++) {
            for(int j = 0; j < comparables.length - i - 1 ; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",20);
        students[1] = new Student("wangwu",40);
        students[2] = new Student("lisi",10);
        System.out.println("排序前："+Arrays.toString(students));
        NameCompareator nameCompareator = new NameCompareator();//用名字排序
        AgeComparator ageComparator = new AgeComparator();//用年龄排序
        Arrays.sort(students,nameCompareator);
        System.out.println("排序后："+Arrays.toString(students));

    }
}
